/* 
 * File:   Control.cpp
 * Author: song
 * 
 * Created on 2010年10月29日, 上午9:08
 */

#include "Control.h"

extern int hdims;
extern int minT;
extern int numFrame;

Control::Control() {
    
    hdims = 256;
    minT = 20;
}

Control::Control(const Control& orig) {
}

Control::~Control() {
}


bool Control::OpenAvi(Camera &cam, char* path)
{
    cam.capture = cvCaptureFromAVI(path);
    if(!cam.capture) {
        cout<<"OpenAvi Failed"<<endl;
        return false;
    }
    return Open(cam);
}

bool Control::Open(Camera& cam)
{
    cam.pFrame = cvQueryFrame(cam.capture);
    if(cam.pFrame == NULL) {
        cout<<"Get first frame failed"<<endl;
        return false;
    }

    numFrame = 1;

    Init(cam);

    //this->RunWindows();
    //this->DebugWindows();

    return true;

    //cvSetCaptureProperty(cam.capture, CV_CAP_PROP_FPS, 20);
}

//第一版中只考虑一个摄像头
bool Control::OpenCam(Camera &cam, int num)
{
    cam.capture = cvCaptureFromCAM(num);
    if(!cam.capture) {
        cout<<"OpenCam Failed"<<endl;
        return false;
    }
    return Open(cam);
}

//第一版中只有一个简单的窗体
void Control::RunWindows()
{
    cvNamedWindow("DetectorVPA1.0", CV_WINDOW_AUTOSIZE);

    cvMoveWindow("DetectorVPA1.0", 400, 400);
}

//调试窗口
void Control::DebugWindows()
{
    cvNamedWindow("bk", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("fr", CV_WINDOW_AUTOSIZE);
    //cvNamedWindow("hist", CV_WINDOW_AUTOSIZE);
    //cvNamedWindow("tmpBk", CV_WINDOW_AUTOSIZE);

    cvMoveWindow("bk", 0, 0);
    cvMoveWindow("fr", 400, 0);
    //cvMoveWindow("hist", 800, 0);
    //cvMoveWindow("tmpBk", 0, 400);

}

//初始化相关数据，之前必须成功
void Control::Init(Camera &cam)
{
        try{
        //程序开发时使用，观察效果
        cam.pGray = cvCreateImage(cvSize(cam.pFrame->width, cam.pFrame->height), IPL_DEPTH_8U, 1);
        cam.tmpBk = cvCreateImage(cvSize(cam.pFrame->width, cam.pFrame->height), IPL_DEPTH_8U, 1);

        //各种图片格式的内存分配
        cam.pBkImg = cvCreateImage(cvSize(cam.pFrame->width, cam.pFrame->height), IPL_DEPTH_8U, 1);
        cam.pFrImg = cvCreateImage(cvSize(cam.pFrame->width, cam.pFrame->height), IPL_DEPTH_8U, 1);
        cam.pPre = cvCreateImage(cvSize(cam.pFrame->width, cam.pFrame->height), IPL_DEPTH_8U, 3);
        cam.pBkMat = cvCreateMat(cam.pFrame->height, cam.pFrame->width, CV_32FC1);
        cam.pFrMat = cvCreateMat(cam.pFrame->height, cam.pFrame->width, CV_32FC1);
        cam.pGrayMat = cvCreateMat(cam.pFrame->height, cam.pFrame->width, CV_32FC1);
        cam.pFrameMat = cvCreateMat(cam.pFrame->height, cam.pFrame->width, CV_32FC1);
        cam.tmpBkMat = cvCreateMat(cam.pFrame->height, cam.pFrame->width, CV_32FC1);

        cvCvtColor(cam.pFrame, cam.pBkImg, CV_BGR2GRAY);
        cvCvtColor(cam.pFrame, cam.pFrImg, CV_BGR2GRAY);
        cvConvert(cam.pFrImg, cam.pFrameMat);
        cvConvert(cam.pFrImg, cam.pFrMat);
        cvConvert(cam.pFrImg, cam.pBkMat);

        float hranges_arr[]={0,180};
        float *hranges = hranges_arr;


        cam.hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );


    }
    catch(...){
        //抛出初始数据错误
    }
}

